package com.atnjupt.admin.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;

/**
 * @author Wizarder
 * @create 2021-10-22 16:49
 */
@Deprecated
//@Configuration
public class MyDataSourceConfig {
    // 默认的自动配置是判断容器中没有才会配@ConditionalOnMissingBean(DataSource.class)
    //和配置文件进行绑定
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource() throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
//        druidDataSource.setUrl();
//        druidDataSource.setUsername();
//        druidDataSource.setPassword();
        //Druid内置提供一个StatFilter，用于统计监控信息。可以通过配置文件进行绑定
//        dataSource.setFilters("stat,wall");
        dataSource.setMaxActive(10);
        return dataSource;
    }

    /**
     * 开启页面监控功能，配置statViewServlet
     * @return
     */
    @Bean
    public ServletRegistrationBean statViewServlet(){
        StatViewServlet statViewServlet = new StatViewServlet();
        ServletRegistrationBean<StatViewServlet> ServletRegistrationBean =
                new ServletRegistrationBean<>(statViewServlet,"/druid/*");
        //给注册的servlet中添加初始化值
        ServletRegistrationBean.addInitParameter("loginUsername","luhu");
        ServletRegistrationBean.addInitParameter("loginPassword","admin");
        return ServletRegistrationBean;
    }

    /**
     * WebStatFilter用于采集web-jdbc关联监控的数据。
     * @return
     */
    @Bean
    public FilterRegistrationBean webStatFilter(){
        WebStatFilter webStatFilter = new WebStatFilter();
        FilterRegistrationBean<WebStatFilter> FilterRegistrationBean = new FilterRegistrationBean<>(webStatFilter);
        FilterRegistrationBean.setUrlPatterns(Arrays.asList("/*"));
        //给注册的filter中添加初始值
        FilterRegistrationBean.addInitParameter("exclusions","*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return FilterRegistrationBean;
    }
}
